package compilers.condition;

import java.io.BufferedWriter;
import java.io.IOException;

import compilers.stmt.Condition;
import compilers.table.MemScope;
import compilers.types.Type;

public class Return extends Condition {
	Condition fNode;
	Type fType; // type of fNode
	Type fShouldBe; // what should the return type be ??
	
	public Return(Condition node) {
		super();
		this.fNode = node;
		if (node instanceof Term) {
			Type t = ((Term)node).compute();
			if (t != null) {
				this.fNode = t;
			}
		}
		if (node instanceof Factor) {
			Type t = ((Factor)node).compute();
			if (t != null) {
				this.fNode = t;
			}
		}
	}

	public void setTypes(Type is, Type should) {
		this.fType = is;
		this.fShouldBe = should;
	}
	
	public void print(int tab) {
		for (int i=1;i<=tab;i++){System.out.print("\t");}System.out.print("|-");
		System.out.println("Return");
		fNode.print(tab+1);
	}

	@Override
	public void generate(BufferedWriter writer, MemScope scope) throws IOException {
		if (this.getfRes() == null) {
			this.setfRes(fShouldBe);
		}
		
		//System.out.println("RETURN is " + fType.getPType() + " should be " + fShouldBe.getPType());
		
		fNode.setfRes(this.getfRes());
		fNode.generate(writer, scope);
		
		writer.write("str " + fShouldBe.getPType() + " 0 0\n");
		writer.write("retf\n");
	}

}
